import { createStore } from 'vuex'
import {login_refresh} from "@/axios/login";
import {setStorage,getStorage} from "@/store/storage";

export default createStore({
  state: {
    token:getStorage({name:"token"}),
    refreshToken:getStorage({name:"refreshToken"}),
  },
  mutations: {
    setToke(state,token){
      state.token = token;
      setStorage({name:"token",val:token})
    },
    setReToke(state, refreshToken){
      state.refreshToken = refreshToken;
      setStorage({name:"refreshToken", val:refreshToken})
    }
  },
  actions: {
      async  refresh_token({commit, state}){
        let params = {};
        params.refresh_token = state.refreshToken;
        await login_refresh(params).then(res=>{
          commit("setToke",res.data.access_token)
          commit("setReToke", res.data.refresh_token)
        })
      }
  },
  modules: {
  }
})
